package gw.com.android.model;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;

import com.gwtsz.gts2.hx.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.presenter.PresenterImpl;
import gw.com.android.presenter.SwitchAccountPresenter;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.chart.ChartTypeData;
import gw.com.android.ui.dialog.LogOutDialog;
import gw.com.android.ui.dialog.LoginDialog;
import gw.com.android.ui.dialog.PopupDoubleBtnDialog;
import gw.com.android.ui.dialog.SelectColorDialog;
import gw.com.android.ui.gesture_pwd.consts.GesturePwdConst;
import gw.com.android.ui.me.FeedbackActivity;
import gw.com.android.ui.system.AppSettingActivity;
import gw.com.android.ui.system.PushSettingActivity;
import gw.com.android.ui.system.SystemSettingAdapter;
import gw.com.android.upgrade.UpgradeHelper;
import gw.com.android.utils.NotificationUtil;
import gw.com.android.utils.PackStatis;
import www.com.library.app.Logger;
import www.com.library.app.ObjectSessionStore;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.NetworkMonitor;
import www.com.library.util.StringFormatter;
import www.com.library.view.BtnClickListener;

/**
 * Created by reeta.zou on 2016/8/3.
 * 系统设置相关界面选项的配置处理
 */
public class ConfigSettingDeal {

    /**
     * 根据key值获取对象
     *
     * @param key
     * @return
     */
    public DataItemDetail getItemValue(String key) {
        DataItemDetail mItem = new DataItemDetail();
        JSONObject jsonObject = ConfigUtil.instance().mConfigObject.optJSONObject(key);
        mItem.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, key);
        if (GTConfig.instance().getLanguage().equals(GTConfig.LANG_ZH_CN)) {
            mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_TYPE_DES_TAG));
        } else {
            mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_TYPE_TW_DES_TAG));
        }
        mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_TYPE_TAG));
        mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_VALUE_TAG));

        return mItem;
    }

    /**
     * 根据url获取网页打开的对象
     *
     * @param title
     * @return
     */
    public DataItemDetail getItemValue(String title, String url) {
        return getItemValue("", title, url);
    }

    /**
     * 根据url获取网页打开的对象
     *
     * @param title
     * @return
     */
    public DataItemDetail getItemValue(String key, String title, String url) {
        DataItemDetail mItem = new DataItemDetail();
        mItem.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, key);
        mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, title);
        mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, ConfigType.CONFIG_TYPE_TYPE_URL_TAG);
        mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, "");
        mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, url);

        return mItem;
    }

    public static DataItemResult getMineList() {
        JSONArray jsonArr = ConfigUtil.instance().mConfigObject.optJSONArray(ConfigType.SYSTEM_LIST_TAG);
        if (jsonArr != null && jsonArr.length() > 0) {
            String accountType;
            if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_GUEST) {
                accountType = ConfigType.GUEST_TAG;
            } else if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO) {
                accountType = ConfigType.DEMO_TAG;
            } else {
                accountType = ConfigType.REAL_TAG;
            }
            DataItemResult result = new DataItemResult();
            for (int i = 0; i < jsonArr.length(); i++) {
                JSONObject obj = jsonArr.optJSONObject(i);
                if (null != obj) {
                    if (!obj.optBoolean(ConfigType.CONFIG_TYPE_HAS_NEED_TAG)) {
                        continue;
                    }
                    String accType = obj.optString(ConfigType.CONFIG_TYPE_ACC_TYPE_TAG);
                    if (accType == null || !accType.contains(accountType)) {
                        continue;
                    }
                    DataItemDetail mItem = new DataItemDetail();
                    mItem.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, obj.optString(ConfigType.CONFIG_TYPE_TITLE_TAG));
                    mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_ICONURL_TAG, obj.optString(ConfigType.CONFIG_TYPE_TYPE_ICONURL_TAG));
                    mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, obj.optString(ConfigType.CONFIG_TYPE_TYPE_TAG));
                    mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, obj.optString(ConfigType.CONFIG_TYPE_TYPE_DES_TAG));
                    result.addItem(mItem);
                }
            }
            return result;
        }
        return null;
    }

    /**
     * 系统设置界面根据类型获取数据
     *
     * @param mGroup
     * @param mTag
     * @return
     */
    public ArrayList<DataItemResult> getSettingList(DataItemResult mGroup, String mTag) {
        return getSystemList(mGroup, mTag);
    }

    public ArrayList<DataItemResult> getSystemList(DataItemResult mGroup, String mTag) {
        ArrayList<DataItemResult> mResult = new ArrayList<DataItemResult>();
        JSONArray array;
        if (mTag.equals(ConfigType.SYSTEM_SETTING_TAG) || ConfigType.SYSTEM_LIST_TAG.equals(mTag)) {
            JSONObject object = ConfigUtil.instance().mConfigObject.optJSONObject(mTag);
            if (null == object) {
                return mResult;
            }
            if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_GUEST) {
                array = object.optJSONArray(ConfigType.GUEST_TAG);
            } else if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO) {
                array = object.optJSONArray(ConfigType.DEMO_TAG);
            } else {
                array = object.optJSONArray(ConfigType.REAL_TAG);
            }
        } else {
            array = ConfigUtil.instance().mConfigObject.optJSONArray(mTag);
        }
        if (null != array && array.length() > 0) {
            getGroupList(array, mGroup, mResult);
        }

        return mResult;
    }

    /**
     * 处理组和子集的值
     *
     * @param mGroup
     * @param mResult
     */
    private void getGroupList(JSONArray array, DataItemResult mGroup, ArrayList<DataItemResult> mResult) {
        DataItemDetail mItem;
        JSONObject obj;
        for (int i = 0; i < array.length(); i++) {
            obj = array.optJSONObject(i);
            if (null != obj) {
                if (!obj.optBoolean(ConfigType.CONFIG_TYPE_HAS_NEED_TAG)) {
                    continue;
                }
                mItem = new DataItemDetail();
                String key = obj.optString(ConfigType.CONFIG_TYPE_TITLE_TAG);
                mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, key);
                mGroup.addItem(mItem);
                JSONArray groupArray = obj.optJSONArray(ConfigType.CONFIG_TYPE_GROUP_TAG);
                if (null != groupArray && groupArray.length() > 0) {
                    mResult.add(getMenuList(groupArray));
                }
            }
        }

//        mGroup.Dump();
    }


    /**
     * 根据配置文件条件返回各菜单的一些选项
     *
     * @param array
     * @return
     */
    private DataItemResult getMenuList(JSONArray array) {
        DataItemResult items = new DataItemResult();
        DataItemDetail mItem = null;
        for (int i = 0; i < array.length(); i++) {
            /*
             * system_setting  my_collect clear_cache about version_update
             * app_des user_guide constom_service
             * zh_CN zh_TW green_up red_up
             * system_language screen_bright color_setting order_sound order_shake
             */
            JSONObject jsonObject = array.optJSONObject(i);
            String key = jsonObject.optString(ConfigType.CONFIG_TYPE_TITLE_TAG);
            if (!jsonObject.optBoolean(ConfigType.CONFIG_TYPE_HAS_NEED_TAG)) {
                continue;
            }
            mItem = new DataItemDetail();
            mItem.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, key);
            if (GTConfig.instance().getLanguage().equals(GTConfig.LANG_ZH_CN)) {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_TYPE_DES_TAG));
            } else {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_TYPE_TW_DES_TAG));
            }
            String type = jsonObject.optString(ConfigType.CONFIG_TYPE_TYPE_TAG);
            mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, type);

            mItem.setStringValue(ConfigType.CONFIG_TYPE_SUB_DES_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_SUB_DES_TAG));
            mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_VALUE_TAG));
            mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_ICONURL_TAG, jsonObject.optString(ConfigType.CONFIG_TYPE_TYPE_ICONURL_TAG));

            //开关
            if (type.equals(ConfigType.CONFIG_TYPE_TYPE_SWITCH_TAG)) {
                setSwitchItemValue(key, mItem);
            } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_NEXTSUB_TAG)) {
                //带箭头，箭头文字
                setSubItemValue(key, mItem);
            } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_PAGE_TAG)) {
                if (key.equals("pushSetting")) {
                    if (NotificationUtil.isNotificationEnabled(AppMain.getApp())) {
                        mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.push_type));
                    } else {
                        mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.push_not_enable));
                    }
                }
            } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_FUNC_TAG)) {
                if (key.equals(ConfigType.COLOR_TAG)) {
                    if (GTConfig.instance().getPrefColorConfig().equals(GTConfig.COLOR_GREEN_RISE)) {
                        mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.greenUp));
                    } else {
                        mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.redUp));
                    }
                }
            }
            items.addItem(mItem);
        }
//        items.Dump();
        return items;
    }

    /**
     * nextSub类型设置各项值
     */
    private void setSubItemValue(String key, DataItemDetail mItem) {
        if (key.equals(ConfigType.LANGUAGE_TAG)) {
            if (GTConfig.instance().getLanguage().equals(GTConfig.LANG_ZH_CN)) {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.zh_CN));
            } else {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.zh_TW));
            }
        } else if (key.equals(ConfigType.COLOR_TAG)) {
            if (GTConfig.instance().getPrefColorConfig().equals(GTConfig.COLOR_GREEN_RISE)) {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.greenUp));
            } else {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.redUp));
            }
        } else if (key.equals(ConfigType.ZONE_TAG)) {
            int zone = AppTerminal.instance().getZoneType();
            if (zone >= 0) {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, "GMT+" + AppTerminal.instance().getZoneType());
            } else {
                mItem.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, "GMT" + AppTerminal.instance().getZoneType());
            }
        }
    }

    /**
     * Switch类型设置各项值
     *
     * @param key
     * @return
     */
    private void setSwitchItemValue(String key, DataItemDetail mItem) {
        if (key.equals(ConfigType.SCREEN_BRIGHT_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_KEEP_SCREEN));
        } else if (key.equals(ConfigType.SOUND_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_ORDER_NOTICE_SOUND, true));
        } else if (key.equals(ConfigType.SHAKE_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_ORDER_NOTICE_SHAKE, true));
        } else if (key.equals(ConfigType.AUTO_LOGIN_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_AUTO_LOGIN, true));
        } else if (key.equals(ConfigType.GESTURE_ID_PWD_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PRE_OPEN_GESTURE, false));
        } else if (key.equals(ConfigType.ORDER_TRADE_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_ORDER_TRADE, true));
        } else if (key.equals(ConfigType.ORDER_QUOTE_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_ORDER_QUOTE, true));
        } else if (key.equals(ConfigType.ORDER_ONLIVE_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_ORDER_ONLIVE, true));
        } else if (key.equals(ConfigType.ORDER_NEWS_TAG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_ORDER_NEWS, true));
        } else if (key.equals(ConfigType.PRICE_FONT_BIG)) {
            mItem.setBooleanValue("isCheck", GTConfig.instance().getBooleanValue(GTConfig.PREF_BIG_QUOTE, false));
        }
    }

    /**
     * 各选项点击事件
     *
     * @param mActivity
     * @param mItem     选项唯一标识
     */
    public void onViewClick(final FragmentActivity mActivity, PresenterImpl fragment, DataItemDetail mItem, String title, SystemSettingAdapter adapter) {
        if (CommonUtils.isFastDoubleClick()) {
            return;
        }
        String type = mItem.getString(ConfigType.CONFIG_TYPE_TYPE_TAG);
        String key = mItem.getString(ConfigType.CONFIG_TYPE_KEY_TAG);
        if (type.equals(ConfigType.CONFIG_TYPE_TYPE_NEXTSUB_TAG) || type.equals(ConfigType.CONFIG_TYPE_TYPE_NEXT_TAG)) {
            ActivityManager.showSettingActivity(mActivity, key, title, mItem.getString(ConfigType.CONFIG_TYPE_TITLE_TAG));
            if (key.equals("systemLanguage")) {
                PackStatis.getToService(PackStatis.EventAction.CLICKLANGUAGE.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            } else if (key.equals("systemZone")) {
                PackStatis.getToService(PackStatis.EventAction.CLICKTIMEZONE.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            } else if (key.equals("soundSetting")) {
                PackStatis.getToService(PackStatis.EventAction.CLICKVOICE.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            } else if (key.equals("aboutUs")) {
                PackStatis.getToService(PackStatis.EventAction.CLICKABOUTUS.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            } else if (key.equals("clearCache")) {
                PackStatis.getToService(PackStatis.EventAction.CLICKCLEAR.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            }
        } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_FUNC_TAG) || type.equals(ConfigType.CONFIG_TYPE_TYPE_CENTER_TAG)) {
            onViewClickFunc(mActivity, key, mItem, adapter);
        } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_PAGE_TAG)) {
            onViewClickPage(mActivity, key, title, mItem);
        } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_CHECK_TAG)) {
            String value = mItem.getString(key);
            onViewClickCheck(mActivity, key, value);
        } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_CUSTOM_TAG)) {
            //String value = mItem.getString(key);
            onViewClickCustom(mActivity, mItem);
        } else if (type.equals(ConfigType.CONFIG_TYPE_TYPE_URL_TAG) || type.equals(ConfigType.CONFIG_TYPE_TYPE_URL_RGS_TAG)) {
            if (!NetworkMonitor.hasNetWork()) {
                return;
            }
            if (("recommended").equals(key)) {
                PackStatis.getToService(PackStatis.EventAction.CLICKREWARD.getValue(), PackStatis.EventCategory.ME.getValue(), null, null);
            }
            if (("bullentin").equals(key)) {
                PackStatis.getToService(PackStatis.EventAction.CLICKNEWS.getValue(), PackStatis.EventCategory.ME.getValue(), null, null);
            }
            if (("myInfo").equals(key)) {
                PackStatis.getToService(PackStatis.EventAction.CLICKSELFINFO.getValue(), PackStatis.EventCategory.ACCOUNTINFO.getValue(), null, null);
            }
            if (("bankInfo").equals(key)) {
                PackStatis.getToService(PackStatis.EventAction.CLICKBACKINFO.getValue(), PackStatis.EventCategory.ACCOUNTINFO.getValue(), null, null);
            }
            if (("uploadInfo").equals(key)) {
                PackStatis.getToService(PackStatis.EventAction.CLICKUPDATEINFO.getValue(), PackStatis.EventCategory.ACCOUNTINFO.getValue(), null, null);
            }
            if (("faqUrl").equals(key)) {
                PackStatis.getToService("clickQA", "Me", null, null);
            }
            if (("newNotice").equals(key)) {
                PackStatis.getToService("clickAttention", "Trade", null, null);
            }
            if ("switchReal".equals(key)) {
                PackStatis.getToService("clickIdentification", "Me", null, null);
                if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO) {
                    if (!NetworkMonitor.hasNetWork()) {
                        return;
                    }
                    if (fragment != null) {
                        SwitchAccountPresenter mSwitchPresenter = new SwitchAccountPresenter(mActivity, fragment);
                        mSwitchPresenter.AccAccountSwitch(GTConfig.ACC_TYPE_REAL, GTConfig.FROM_NEEDLOGIN);
                    }
//				SwitchAccountPresenter.instance().realAccSwitch(getActivity(),GTConfig.ACC_TYPE_REAL,this);
                } else {
                    LoginDialog.showLoginDialog(mActivity, ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1014));
                }
                PackStatis.getToService(PackStatis.EventAction.CLICKRACCOUNT.getValue(), PackStatis.EventCategory.ME.getValue(), null, null);
                return;
            }
            ActivityManager.showWebPageActivity(mActivity, mItem, title);
        }
        if (key.equals("myInfo")) {
            PackStatis.getToService(PackStatis.EventAction.CLICKSELFINFO.getValue(), PackStatis.EventCategory.ACCOUNTINFO.getValue(), null, null);
        } else if (key.equals("bankInfo")) {
            PackStatis.getToService(PackStatis.EventAction.CLICKBACKINFO.getValue(), PackStatis.EventCategory.ACCOUNTINFO.getValue(), null, null);
        } else if (key.equals("uploadInfo")) {
            PackStatis.getToService(PackStatis.EventAction.CLICKUPDATEINFO.getValue(), PackStatis.EventCategory.ACCOUNTINFO.getValue(), null, null);
        }
    }

    /**
     * switch类型处理事件
     *
     * @param mActivity
     * @param mItem     选项唯一标识
     * @param isOpen    开关
     */
    public void onViewClickSwitch(final FragmentActivity mActivity, DataItemDetail mItem, boolean isOpen) {
        if (CommonUtils.isFastDoubleClick()) {
            return;
        }
        String key = mItem.getString(ConfigType.CONFIG_TYPE_KEY_TAG);
        if (key.equals(ConfigType.SCREEN_BRIGHT_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_KEEP_SCREEN, isOpen);
            if (GTConfig.instance().getBooleanValue(GTConfig.PREF_KEEP_SCREEN, false)) {
                mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            } else {
                mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
            PackStatis.getToService(PackStatis.EventAction.CLICKALWAYSLIGHT.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
        } else if (key.equals(ConfigType.SOUND_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_ORDER_NOTICE_SOUND, isOpen);
        } else if (key.equals(ConfigType.SHAKE_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_ORDER_NOTICE_SHAKE, isOpen);
        } else if (key.equals(ConfigType.AUTO_LOGIN_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_AUTO_LOGIN, isOpen);
            if (isOpen) {
                PackStatis.getToService(PackStatis.EventAction.CLICKAUTOLOGIN.getValue(), PackStatis.EventCategory.SETTING.getValue(),
                        null, "1");
            } else {
                PackStatis.getToService(PackStatis.EventAction.CLICKAUTOLOGIN.getValue(), PackStatis.EventCategory.SETTING.getValue(),
                        null, "2");
            }
        } else if (key.equals(ConfigType.GESTURE_ID_PWD_TAG)) {//手势密码
            GTConfig.instance().setBooleanValue(GTConfig.PRE_OPEN_GESTURE, isOpen);
            Log.d("gestureTag", "点击了开关:" + isOpen);

        } else if (key.equals(ConfigType.ORDER_TRADE_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_ORDER_TRADE, isOpen);
        } else if (key.equals(ConfigType.ORDER_QUOTE_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_ORDER_QUOTE, isOpen);
        } else if (key.equals(ConfigType.ORDER_ONLIVE_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_ORDER_ONLIVE, isOpen);
        } else if (key.equals(ConfigType.ORDER_NEWS_TAG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_ORDER_NEWS, isOpen);
        } else if (key.equals(ConfigType.PRICE_FONT_BIG)) {
            GTConfig.instance().setBooleanValue(GTConfig.PREF_BIG_QUOTE, isOpen);
        }

    }

    /**
     * Func类型处理事件
     *
     * @param mActivity
     * @param key       选项唯一标识
     */
    private void onViewClickFunc(final FragmentActivity mActivity, String key, final DataItemDetail item, final SystemSettingAdapter adapter) {
        if (key.equals(ConfigType.COLOR_TAG)) {
            PackStatis.getToService(PackStatis.EventAction.CLICKCOLORTYPE.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            new SelectColorDialog(mActivity).setColorListener(new SelectColorDialog.OnColorSelectedListener() {
                @Override
                public void onSelect(String color) {
                    if (color.equals(GTConfig.COLOR_GREEN_RISE)) {
                        item.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.greenUp));
                        if (adapter != null) {
                            adapter.notifyDataSetChanged();
                        }
                    } else if (color.equals(GTConfig.COLOR_RED_RISE)) {
                        item.setStringValue(ConfigType.CONFIG_TYPE_VALUE_TAG, AppMain.getAppString(R.string.redUp));
                        if (adapter != null) {
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            }).show();
        } else if (key.equals(ConfigType.CLEAR_CACHE_TAG)) {
            PackStatis.getToService(PackStatis.EventAction.CLICKCLEAR.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            PopupDoubleBtnDialog dialog = PopupDoubleBtnDialog.getAppClearData((BaseActivity) mActivity);
            dialog.show();
        } else if (key.equals(ConfigType.VERSION_UPDATE_TAG)) {
            // 未连上网络时,气泡提示
            if (!NetworkMonitor.hasNetWork()) {
                return;
            }
            // 检测版本更新
            UpgradeHelper.getInstance().reStart();
        } else if (key.equals(ConfigType.EXIT_TAG)) {
            PackStatis.getToService(PackStatis.EventAction.CLICKLOGOUT.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
            /*PopupDoubleBtnDialog dialog = PopupDoubleBtnDialog.getAppQuit(mActivity);
            dialog.show();*/
            new LogOutDialog().show(mActivity.getSupportFragmentManager(), "");
        }
    }

    /**
     * page类型处理事件
     *
     * @param mActivity
     * @param key       选项唯一标识
     */
    private void onViewClickPage(final FragmentActivity mActivity, String key, String mBack, DataItemDetail mItem) {
        String mTitle = mItem.getString(ConfigType.CONFIG_TYPE_TITLE_TAG);
        if (key.equals(ConfigType.APP_DES_TAG)) {
            ActivityManager.showDesActivity(mActivity, mBack, mTitle);
        } else if (key.equals(ConfigType.USER_GUIDE_TAG)) {
            ActivityManager.showHelpActivity(mActivity, false, mBack, mTitle);
        } else if (key.equals(ConfigType.GESTURE_ID_PWD_PAGE_TAG)) {//手势密码
            Log.d("gestureTag", "点击了 修改手势密码，现在准备进入手势密码的设置界面");
            try {
                ActivityManager.dispatchGesture("dispatchGestureTag1:byUser", mActivity, GesturePwdConst.MODE_RESET);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (key.equals(ConfigType.MODIFY_PASS_TAG)) {
            ActivityManager.showModifyActivity(mActivity, mBack);
            PackStatis.getToService(PackStatis.EventAction.CLICKCHANGEPASSWORD.getValue(), PackStatis.EventCategory.SETTING.getValue(), null, null);
        } else if (key.equals(ConfigType.PRIVACY_CLAUSE_TAG)) {
            ActivityManager.showPrivacyActivity(mActivity, mBack, mTitle);
        } else if (key.equals(ConfigType.BULLENTIN_TYPE_TAG)) {
//            Intent intent = new Intent(mActivity, NotifyCentreActivity.class);
//            mActivity.startActivity(intent);
            ActivityManager.showBulletinActivity(mActivity, null);
        } else if (key.equals(ConfigType.OPEN_ACCOUNT_TAG)) {
            ActivityManager.showOpenAccount(mActivity, ConfigType.ADD_LOGIN_REAL_TAG);
        } else if (key.equals(ConfigType.FEEDBACK_TAG)) {
            PackStatis.getToService("clickhelp2", "Me", null, null);
            mActivity.startActivity(new Intent(mActivity, FeedbackActivity.class));
        } else if (key.equals("pushSetting")) {
            if (NotificationUtil.isNotificationEnabled(mActivity)) {
                PackStatis.getToService("clickReceive2", "Me", null, null);
                mActivity.startActivity(new Intent(mActivity, PushSettingActivity.class));
            } else {
                PackStatis.getToService("clickReceive1", "Me", null, null);
                NotificationUtil.toNotificationSetting(mActivity, AppContances.REQUEST_SYSTEM_PUSH_CODE);
            }
        }

    }

    /**
     * Check类型处理事件
     *
     * @param mActivity
     * @param key       选项唯一标识
     */
    private void onViewClickCheck(final FragmentActivity mActivity, String key, String value) {
        if (key.equals(ConfigType.ZH_CN_TAG) || key.equals(ConfigType.ZH_TW_TAG)) {
            //简繁体切换
            if (key.equals(ConfigType.ZH_CN_TAG)) {
                GTConfig.instance().saveLanguage(GTConfig.LANG_ZH_CN);
                //繁体
            } else if (key.equals(ConfigType.ZH_TW_TAG)) {
                GTConfig.instance().saveLanguage(GTConfig.LANG_ZH_TW);
            }
            AppTerminal.instance().setLanguage(GTConfig.instance().getLanguage());
            GTConfig.instance().configureLanguage(mActivity);
            ConfigUtil.instance().changeLanguage();
            ChartTypeData.reset();
//			//设置语言标签
            GTConfig.instance().setJpushAliasAndTags(null, "");
            MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
            if (mainActivity != null) {
                mainActivity.finish();
            }
            if (AppSettingActivity.mInstance != null) {
                AppSettingActivity.mInstance.finish();
            }

            ActivityManager.showMainTab(mActivity, ConfigType.TAB_MYSELF_TAG, AppContances.FROM_LANGUAGE_ACTIVITY);
            mActivity.finish();

        } else {
            //时区处理
            Logger.e("key == " + Integer.valueOf(key));
            AppTerminal.instance().setZoneType(Integer.valueOf(key));
            StringFormatter.instance().updateZone(Integer.valueOf(key));
            mActivity.finish();
        }

    }

    public static void showCallDialog(final Activity mActivity, String phoneNum) {
        if (TextUtils.isEmpty(phoneNum)) {
            return;
        }
        final String tel = phoneNum.replace("(", "").replace(")", "");
        PopupDoubleBtnDialog dialog = PopupDoubleBtnDialog.newInstance(mActivity, "", tel, AppMain.getAppString(R.string.btn_call), AppMain.getAppString(R.string.btn_cancel),
                new BtnClickListener() {

                    @Override
                    public void onBtnClick(int btnID) {
                        if (btnID == R.id.action_btn_pos) {
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tel));
                            mActivity.startActivity(intent);
                        }
                    }
                });
        dialog.show();
    }

    private void showOpenWeChatDialog(final Activity mActivity, String weChatId) {

        ClipboardManager cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setPrimaryClip(ClipData.newPlainText(null, weChatId));

        PopupDoubleBtnDialog dialog = PopupDoubleBtnDialog.newInstance(mActivity, "", AppMain.getAppString(R.string.me_aboutus_wechat_title), AppMain.getAppString(R.string.btn_fine), AppMain.getAppString(R.string.btn_cancel),
                new BtnClickListener() {

                    @Override
                    public void onBtnClick(int btnID) {
                        if (btnID == R.id.action_btn_pos) {
                            try {
                                Intent intent = new Intent();
                                ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
                                intent.setAction(Intent.ACTION_MAIN);
                                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setComponent(cmp);
                                mActivity.startActivity(intent);
                            } catch (ActivityNotFoundException exception) {
                                ((BaseActivity) mActivity).showToastPopWindow(AppMain.getAppString(R.string.customer_qq_not_install));
                            }
                        }
                    }
                });
        dialog.show();
    }

    /**
     * Custom类型处理事件
     *
     * @param mItem 当前点击选项的对象
     */
    private void onViewClickCustom(final FragmentActivity mActivity, DataItemDetail mItem) {
        String value = mItem.getString(ConfigType.CONFIG_TYPE_VALUE_TAG);
        value = ConfigUtil.instance().mConfigObject.optString(value);

        String key = mItem.getString(ConfigType.CONFIG_TYPE_KEY_TAG);
        if (key.equals(ConfigType.PHONE_TAG) || key.equals(ConfigType.CLOSE_ORDER_NUMBER_TAG)) {
            final String tel = value.replace("(", "").replace(")", "");
            showCallDialog(mActivity, tel);
        } else if (key.equals(ConfigType.EMAIL_TAG)) {
            try {
                final String email = value;
                Intent data = new Intent(Intent.ACTION_SENDTO);
                data.setData(Uri.parse("mailto:" + email));
                mActivity.startActivity(data);
            } catch (ActivityNotFoundException exception) {
                ((BaseActivity) mActivity).showToastPopWindow(AppMain.getAppString(R.string.customer_mail_not_value));
            }
        } else if (key.equals(ConfigType.WE_CHAT_TAG)) {
            showOpenWeChatDialog(mActivity, value);
        } else if (key.equals(ConfigType.QQ_SERVICE_TAG)) {
            // QQ客服
            PopupDoubleBtnDialog.openQQ(mActivity);
        } else if (key.equals(ConfigType.CONTACT_SERVICE_TAG)) {
            // 在线客服
            ConfigSettingDeal mDeal = new ConfigSettingDeal();
            mDeal.openQQ(mActivity, ConfigType.QQ_SOURCE_ABOUT);
        }
    }

    /**
     * 打开qq客服
     *
     * @param mActivity
     * @return
     */
    public void openQQ(Activity mActivity) {
        openQQ(mActivity, null);
    }

    public void openQQ(Activity mActivity, String from) {
        DataItemDetail item = new DataItemDetail();
        item.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, ConfigType.QQ_SERVICE_TAG);
        item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, ConfigType.CONFIG_TYPE_TYPE_URL_TAG);
        item.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, AppMain.getAppString(R.string.system_contact));
        if (!TextUtils.isEmpty(from)) {
            item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_PARAMS_TAG, "enterurl=" + from);
        }
        ActivityManager.showWebPageActivity(mActivity, item, AppMain.getAppString(R.string.btn_back));
    }

    /**
     * 打开客服
     *
     * @param mActivity
     * @return
     */
    public void openContact(FragmentActivity mActivity, String key) {
        DataItemDetail item = new DataItemDetail();
        item.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, key);
        item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, ConfigType.CONFIG_TYPE_TYPE_URL_TAG);
        item.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, AppMain.getAppString(R.string.system_contact));
        ActivityManager.showWebPageActivity(mActivity, item, AppMain.getAppString(R.string.btn_back));
    }

    /**
     * 打开抽奖活动
     *
     * @param mActivity
     * @return
     */
    public void openLottery(FragmentActivity mActivity, String key) {
        DataItemDetail item = new DataItemDetail();
        item.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, key);
        item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, ConfigType.CONFIG_TYPE_TYPE_URL_TAG);
        item.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, AppMain.getAppString(R.string.system_ads_detail));
        ActivityManager.showWebPageActivity(mActivity, item, AppMain.getAppString(R.string.btn_back));
    }

    /**
     * 根据时区返回时区的title
     *
     * @param zoneType
     * @return
     */
    private int getZoneTitle(int zoneType) {
        switch (zoneType) {
            case 0:
                return R.string.GMT_0;
            case 1:
                return R.string.GMT_1;
            case 2:
                return R.string.GMT_2;
            case 3:
                return R.string.GMT_3;
            case 4:
                return R.string.GMT_4;
            case 5:
                return R.string.GMT_5;
            case 6:
                return R.string.GMT_6;
            case 7:
                return R.string.GMT_7;
            case 8:
                return R.string.GMT_8;
            case 9:
                return R.string.GMT_9;
            case 10:
                return R.string.GMT_10;
            case 11:
                return R.string.GMT_11;
            case 12:
                return R.string.GMT_12;
            case -1:
                return R.string.GMT_1_1;
            case -2:
                return R.string.GMT_1_2;
            case -3:
                return R.string.GMT_1_3;
            case -4:
                return R.string.GMT_1_4;
            case -5:
                return R.string.GMT_1_5;
            case -6:
                return R.string.GMT_1_6;
            case -7:
                return R.string.GMT_1_7;
            case -8:
                return R.string.GMT_1_8;
            case -9:
                return R.string.GMT_1_9;
            case -10:
                return R.string.GMT_1_10;
            case -11:
                return R.string.GMT_1_11;
            case -12:
                return R.string.GMT_1_12;
        }

        return R.string.GMT_0;

    }
}
